let arr = [
    {"id":964,"knowledgeId":null,"question":"parent 1","simpleQuestion":["23"],"answer":"24","tag":["c2"],"trainingState":null,"verifyState":1,"createDate":null,"rootId":964,"treeLevel":1,"templateTypeId":null,"audioPath":null,"createUserId":null,"verifyUserId":null,"secondQuestionFlag":false,"remarks":"234","channel":"1","orgCode":"yiwei","pid":0,"pId":0},
    {"id":966,"knowledgeId":null,"question":"appended node1","simpleQuestion":["2"],"answer":"","tag":["c2"],"trainingState":null,"verifyState":1,"createDate":null,"rootId":964,"treeLevel":2,"templateTypeId":null,"audioPath":null,"createUserId":null,"verifyUserId":null,"secondQuestionFlag":false,"remarks":"","channel":"1","orgCode":"yiwei","pid":964,"pId":964},
    {"id":967,"knowledgeId":null,"question":"appended node22","simpleQuestion":[""],"answer":"","tag":["c2","c3"],"trainingState":null,"verifyState":1,"createDate":null,"rootId":964,"treeLevel":3,"templateTypeId":null,"audioPath":null,"createUserId":null,"verifyUserId":null,"secondQuestionFlag":false,"remarks":"","channel":"1","orgCode":"yiwei","pid":966,"pId":966},
    {"id":968,"knowledgeId":null,"question":"appended node2","simpleQuestion":["2"],"answer":"","tag":["c3"],"trainingState":null,"verifyState":1,"createDate":null,"rootId":964,"treeLevel":2,"templateTypeId":null,"audioPath":null,"createUserId":null,"verifyUserId":null,"secondQuestionFlag":false,"remarks":"","channel":"1","orgCode":"yiwei","pid":964,"pId":964},
    {"id":969,"knowledgeId":null,"question":"appended node333","simpleQuestion":[""],"answer":"","tag":["c2"],"trainingState":null,"verifyState":1,"createDate":null,"rootId":964,"treeLevel":4,"templateTypeId":null,"audioPath":null,"createUserId":null,"verifyUserId":null,"secondQuestionFlag":false,"remarks":"","channel":"1","orgCode":"yiwei","pid":967,"pId":967}
];


let arr1 = [
    {id: 1, pId: 0},
    {id: 2, pId: 1},
    {id: 3, pId: 1},
    {id: 4, pId: 3},
    {id: 5, pId: 0},
    {id: 6, pId: 5},
    {id: 7, pId: 5},
    {id: 8, pId: 7},
    {id: 9, pId: 8},
    {id: 10, pId: 9}
];

function setRealTree(source, id, parentId, children){
    let cloneData = JSON.parse(JSON.stringify(source))
    return cloneData.filter(father => {
        let branchArr = cloneData.filter(child => father[id] === child[parentId]);
        branchArr.length > 0 ? father[children] = branchArr : ''
        return father[parentId] === 0;
    });
}

let res = setRealTree(arr1, 'id', 'pId', 'children');
//console.log(JSON.parse(JSON.stringify(res)));
console.log(JSON.stringify(res));



